/***
 * 2019-06-08 修复插入浮动图片，遗漏onSuccess回调的bug
 * 富文本的常量等定义，该文件作为合并文件的头部文件
 * @Author: kevin.huang 
 * ****/
'use strict';
var VERVar = '_ver_var_';
var VERSite = '_ver_site_';
var VERDate = '_ver_date_';

if (navigator.userAgent.indexOf("Firefox") > 0) {
    document.designMode = "on";
    document.execCommand('enableObjectResizing', false, 'false');
    document.designMode = "off";
}
function fixIeResizeBug(el) {
    if ($B.isIE()) {
        el.onresizestart = function (e) {
            return false;
        };
    }
}
var chSeqArray = ["一", "二", "三", "四", "五", "六", "七", "八", "九", "十"];
var tmpChSeqArray = [], chSeqItem, tmpChSeq;
chSeqItem = "十";
for (var _j = 0; _j < 9; _j++) {
    tmpChSeq = chSeqItem + chSeqArray[_j];
    tmpChSeqArray.push(tmpChSeq);
}
for (var _k = 1; _k < 9; _k++) {
    chSeqItem = chSeqArray[_k] + "十";
    tmpChSeqArray.push(chSeqItem);
    for (_j = 0; _j < 9; _j++) {
        tmpChSeq = chSeqItem + chSeqArray[_j];
        tmpChSeqArray.push(tmpChSeq);
    }
}
chSeqArray = chSeqArray.concat(tmpChSeqArray);
tmpChSeqArray = undefined;
/***
 * 获取下一个中文序列的chSeqArray数组下标
 * chSeq:当前序列
 * return {
 *      index : chSeqArray 下标
 *      seq : 序列
 * }
 * ***/
function getChNextSeq(chSeq) {
    var index, seq;
    for (var i = 0; i < 98; ++i) {
        if (chSeq === chSeqArray[i]) {
            index = i + 1;
            seq = chSeqArray[index];
        }
    }
    return {
        index: index,
        seq: seq
    };
}
/**
 * 获取中文序列的下标
 * ***/
function getChSeqIndex(chSeq) {
    var index;
    for (var i = 0; i < 98; ++i) {
        if (chSeq === chSeqArray[i]) {
            index = i;
        }
    }
    return index;
}
var testSeq = "^[\\s*\\u200b]*(\\d+|[一二三四五六七八九十]+)\\s*$";
var seqexp = "^\\s*[\\(（\\s*]*((\\d+|[一二三四五六七八九十]+)\\s*[\\.、\\)）])\\s*";
var elementToTargets = {
    "SPAN": true,
    "TD": true,
    "A": true
};
var titleArray = [{
    txt: '正文',
    v: {
        "line-height": "25.2px",
        "font-size": "14px",
        "font-weight": "400"
    }
}, {
    txt: '标题1',
    v: {
        "line-height": "50.4px",
        "font-size": "28px",
        "font-weight": "700"
    }
}, {
    txt: '标题2',
    v: {
        "line-height": "43.2px",
        "font-size": "24px",
        "font-weight": "700"
    }
}, {
    txt: '标题3',
    v: {
        "line-height": "39.6px",
        "font-size": "22px",
        "font-weight": "700"
    }
}, {
    txt: '标题4',
    v: {
        "line-height": "36px",
        "font-size": "20px",
        "font-weight": "700"
    }
}, {
    txt: '标题5',
    v: {
        "line-height": "32.4px",
        "font-size": "18px",
        "font-weight": "700"
    }
}, {
    txt: '标题6',
    v: {
        "line-height": "28.8px",
        "font-size": "16px",
        "font-weight": "700"
    }
}, {
    txt: '标题7',
    v: {
        "line-height": "25.2px",
        "font-size": "14px",
        "font-weight": "700"
    }
}
];
var fontsFamilyMap = [{
    txt: "微软雅黑",
    v: "微软雅黑,Microsoft YaHei"
}, {
    txt: "华文宋体",
    v: "宋体,SimSun"
},
{
    txt: "华文楷体",
    v: "楷体,楷体_GB2312,SimKai"
}, {
    "txt": "黑体",
    v: "黑体,SimHei"
},
{
    txt: "隶书",
    v: "隶书,SimLi"
}, {
    txt: "Comic Sans MS",
    v: "Comic Sans MS"
},
{
    txt: "Verdana",
    v: "Verdana"
}, {
    txt: "Arial",
    v: "Arial"
}
];
var fontsMap = [{
    txt: "8号",
    v: "11px"
}, {
    "txt": "9号",
    v: "12px"
},
{
    txt: "10号",
    v: "13px"
}, {
    txt: "11号",
    v: "14px"
},
{
    txt: "12号",
    v: "16px"
}, {
    txt: "13号",
    v: "18px"
},
{
    txt: "14号",
    v: "20px"
}, {
    txt: "15号",
    v: "22px"
},
{
    txt: "16号",
    v: "24px"
}, {
    txt: "17号",
    v: "28px"
}, {
    txt: "18号",
    v: "32px"
}
];
var legalCssStyleNames = ['border-width', 'background','border-style', 'border-color', 'line-through', 'width', 'height', 'border-bottom', 'border-top', 'border-right', 'border-left', 'border-collapse', 'font-family', 'font-size', 'font-weight', 'font-style', 'text-decoration', 'background-color', 'color', 'text-align', 'list-style-position'];
var pLegalCssStyleNames = ['line-height','background', 'padding-top', 'padding-bottom', 'padding-right', 'padding-left', 'padding', 'margin-right', 'margin-left', 'margin-bottom', 'margin-top', 'margin', 'border-top', 'border-right', 'border-bottom', 'border-left', 'border', 'text-indent'];
var legalFontFamilys = ['宋体', '微软雅黑', '黑体', '楷体', 'Arial', 'Verdana', '"Times New Roman"', 'Consolas'];
var fontSizePt2Px = {
    '7.5': '10px',
    '8': '11px',
    '9': '12px',
    '10': '12.5px',
    '10.5': '14px',
    '11': '14.7px',
    '12': '16px',
    '13': '17px',
    '13.5': '18px',
    '14': '18.7px',
    '14.5': '19px',
    '15': '20px',
    '16': '21.3px',
    '17': '23px',
    '18': '24px',
    '20': '26.7px',
    '22': '29.3px',
    '24': '32px',
    '26': '34.7px',
    '27': '36px',
    '28': '37.3px',
    '29': '38px',
    '30': '40px',
    '32': '42px',
    '34': '45px',
    '36': '48px',
    '42': '56px',
    '48': '64px',
    '72': '96px'
};
var legalFontWeights = ['bold', 'normal', '700', '400'];
var legalFontStyles = ['italic', 'normal'];
var legalTextDecorations = ['line-through', 'underline', 'none'];
var legalTextAligns = ['center', 'left', 'right', 'justify'];
var legalAttr = ["type", "closed", "closed_count", "closed_status", "id", "class", "style", "tabindex", "w", "h", "col", "row", "href", "src", "target", "colspan", "rowspan", "contenteditable"];

var cssloadString;
/**提取当前页面中需要用到的css链接**/
function getCssloadString(flowScriptSrcs) {
    if (!flowScriptSrcs && cssloadString) {
        return cssloadString;
    }    
    var cssPath = [];
    $("link.k_bui_css").each(function () {
        cssPath.push($(this).attr("href"));
    });
    if(flowScriptSrcs){
        cssPath.push(flowScriptSrcs.flowcss);        
    }
    var linkArr = [],i,len,path;
    linkArr.push('<body onload="' + 'var d=document;var head=d.getElementsByTagName(\'head\')[0];var link;');
    for (i = 0, len = cssPath.length; i < len; ++i) {
         path = cssPath[i];
        linkArr.push('link=d.createElement(\'link\');link.type=\'text/css\';link.rel=\'stylesheet\';link.href=\'' + path + '\';head.appendChild(link);');
    }
    if(flowScriptSrcs){      
        var jsPath =  flowScriptSrcs.src;      
        for (i = 0, len = jsPath.length; i < len; ++i) {
            path = jsPath[i];
            linkArr.push('s=d.createElement(\'script\');s.defer=\'defer\';s.type=\'text/javascript\';s.src=\'' + path + '\';head.appendChild(s);');
        }
    }
    linkArr.push('">');
    if(flowScriptSrcs){
        linkArr.push(flowScriptSrcs.struct);
    }
    linkArr.push('</body>');
    if(flowScriptSrcs){
        linkArr.push(flowScriptSrcs.onloadScript);
    }
    var resouce = linkArr.join("");
    if(!flowScriptSrcs){
        cssloadString = resouce;
    }
    return resouce;
}
function getUUID() {
    return "k_" + $B.generateDateUUID("ddhhmmssSS", 10);
}
var eConfig = $B.editConfig;
var $body;
function _getBody() {
    if (!$body) {
        $body = $(window.document.body).css("position", "relative");
    }
    return $body;
}
//将默认的defaultStyle 转为Object
var styleArray = eConfig.defaultStyle.split(";");
var defaultStyleObject = {}, _s1, tmpS1Arr;
for (var startI = 0, tmpLen = styleArray.length; startI < tmpLen; ++startI) {
    _s1 = styleArray[startI];
    if (_s1 !== "") {
        tmpS1Arr = _s1.split(":");
        defaultStyleObject[$.trim(tmpS1Arr[0])] = $.trim(tmpS1Arr[1]).toLowerCase();
    }
}
/**临时阻止默认的右键***/
var stopDefaultctxMenu = function (e) {
    _getBody().on("contextmenu.edit", function () {
        return false;
    });
    setTimeout(function () {
        _getBody().off("contextmenu.edit");
    }, 800);
};
/**
 * 公共变量
 * **/
var head = $("head");
var userAgent = navigator.userAgent;
var selectDataKey = "_exist_*_";
var sectionTag = "DIV";
var extCopyCfg = {
    "": 1,
    "line-height": 1,
    "text-indent": 1,
    "margin-top": 1,
    "margin-bottom": 1,
    "border-width": 1,
    "border-color": 1,
    "border-style": 1
};
var config = {
    tools: {
        save: {
            icon: 'fa-floppy',
            params: {
                undo: false,
                needRange: false
            }
        },
        paintBrush: {
            icon: 'fa-brush',
            params: {
                undo: true,
                needRange: true
            }
        },
        clear: {
            icon: 'fa-trash-empty',
            params: {
                undo: true,
                needRange: false
            }
        },
        source: {
            icon: 'fa-code',
            params: {
                undo: false,
                needRange: false
            }
        },
        undo: {
            icon: 'fa-ccw-1',
            params: {
                undo: false,
                needRange: false
            },
            disabled: true
        },
        redo: {
            icon: 'fa-cw-1',
            params: {
                undo: false,
                needRange: false
            },
            disabled: true
        },
        bold: {
            icon: 'fa-bold',
            params: {
                undo: true
            }
        },
        italic: {
            icon: 'fa-italic',
            params: {
                undo: true
            }
        },
        underline: {
            icon: 'fa-underline',
            params: {
                undo: true
            }
        },
        strikethrough: {
            icon: 'fa-strike',
            params: {
                undo: true
            }
        },
        superscript: {
            icon: 'fa-superscript',
            params: {
                undo: true
            }
        },
        subscript: {
            icon: 'fa-subscript',
            params: {
                undo: true
            }
        },
        fontFamily: {
            icon: 'fa-angle-down',
            click: 'showFontFamilyPanel',
            params: {
                undo: true
            }
        },
        fontSize: {
            icon: 'fa-angle-down',
            click: 'showFontSizePanel',
            params: {
                undo: true
            }
        },
        fontTitle: {
            icon: 'fa-angle-down',
            click: 'showTitlePanel',
            params: {
                undo: true
            }
        },
        fontSpace: {
            icon: 'fa-text-width-1',
            click: 'showFontSpacePanel',
            params: {
                undo: true
            }
        },
        fontColor: {
            icon: 'fa-pencil',
            click: 'showColorPickerPanel',
            params: {
                delayUndo: true,
                delay: 1000
            }
        },
        backgroundColor: {
            icon: 'fa-pencil-squared',
            click: 'showColorPickerPanel',
            params: {
                delayUndo: true,
                delay: 1000
            }
        },
        paragraph: {
            icon: 'fa-doc-text',
            click: 'showParagraphPanel',
            params: {
                undo: true,
                needRange: false,
                createTargetsByRange: true
            }
        },
        insertTable: {
            icon: 'fa-table',
            click: 'showTablePanel',
            params: {
                undo: true,
                needRange: false
            }
        },
        eraser: {
            icon: 'fa-eraser',
            params: {
                undo: true,
                needRange: false
            }
        },
        orderList: {
            icon: 'fa-list-numbered',
            click: "showOrderListPanel",
            params: {
                undo: true,
                needRange: false
            }
        },
        unorderList: {
            icon: 'fa-list-bullet',
            click: "showUnorderListPanel",
            params: {
                undo: true,
                needRange: false
            }
        },
        alignCenter: {
            icon: 'fa-align-center',
            params: {
                undo: true,
                needRange: false,
                css: {
                    "text-align": "center"
                }
            }
        },
        alignJustify: {
            icon: 'fa-align-justify',
            params: {
                undo: true,
                needRange: false,
                css: {
                    "text-align": "justify"
                }
            }
        },
        alignLeft: {
            icon: 'fa-align-left',
            params: {
                undo: true,
                needRange: false,
                css: {
                    "text-align": "left"
                }
            }
        },
        alignRight: {
            icon: 'fa-align-right',
            params: {
                undo: true,
                needRange: false,
                css: {
                    "text-align": "right"
                }
            }
        },
        link: {
            icon: 'fa-link',
            params: {
                undo: false,
                needRange: false
            }
        },
        picture: {
            icon: 'fa-file-image',
            click: 'showInsertImage',
            params: {
                undo: false,
                needRange: false
            }
        },
        upload: {
            icon: 'fa-upload',
            click: 'showUploadFile',
            params: {
                undo: false,
                needRange: false
            }
        },
        emotion: {
            icon: 'fa-smile',
            click: 'showEmotion',
            params: {
                undo: false,
                needRange: false
            }
        },
        video: {
            icon: 'fa-video',
            click: 'showUploadVideo',
            params: {
                undo: false,
                needRange: false
            }
        },
        split: {
            icon: 'fa-minus',
            params: {
                undo: false,
                needRange: false
            }
        },
        insertIcon: {
            icon: 'fa-heart-empty',
            click: 'showIcon',
            params: {
                undo: false,
                needRange: false
            }
        },
        map: {
            icon: 'fa-location-alt',
            click: 'showMap',
            params: {
                undo: false,
                needRange: false
            }
        },
        insertCode: {
            icon: 'fa-file-code',
            click: 'showCode',
            params: {
                undo: false,
                needRange: false
            }
        },
        insertFloat: {
            icon: 'fa-docs',
            click: 'showFloatPanel',
            params: {
                undo: true
            }
        },
        mobile: {
            icon: ' fa-login-1',
            click: "showPhoneListPanel",
            params: {
                undo: false,
                needRange: false
            }
        },
        flow:{
            icon: 'fa-shuffle',
            params: {
                undo: false,
                needRange: false
            }
        },
        preview: {
            icon: 'fa-eye',
            params: {
                undo: false,
                needRange: false
            }
        },
        fullprint: {
            icon: 'fa-resize-full',
            params: {
                undo: false,
                needRange: false
            }
        },
        help: {
            icon: 'fa-question',
            params: {
                undo: false,
                needRange: false
            }
        }
    },
    mouseMenu: [{
        icon: 'fa-table',
        title: eConfig.label.insertTable,
        handler: 'insertTable',
        params: {
            undo: true,
            needRange: false,
            col: 3,
            row: 3
        }
    }, {
        icon: 'fa-file-image',
        title: eConfig.label.insertImage,
        handler: 'showInsertImageWindow',
        params: {
            undo: true,
            needRange: false
        }
    }, {
        icon: 'fa-upload',
        title: eConfig.label.insertFile,
        handler: 'showInsertFileWindow',
        params: {
            undo: true,
            needRange: false
        }
    }, {
        icon: 'fa-link',
        title: eConfig.label.insertLink,
        handler: 'link',
        params: {
            undo: false,
            needRange: false
        }
    }, {
        icon: 'fa-edit',
        title: eConfig.label.editLink,
        handler: 'editLink',
        params: {
            undo: false,
            needRange: false
        }
    }, {
        icon: 'fa-unlink',
        title: eConfig.label.unLink,
        handler: 'unlink',
        params: {
            undo: false,
            needRange: false
        }
    }, {
        icon: 'fa-smile',
        title: eConfig.label.insertEmotion,
        handler: 'showEmotionWindow',
        params: {
            undo: false,
            needRange: false
        }
    }, {
        icon: 'fa-exchange',
        title: eConfig.label.setSectionWidth,
        tip: eConfig.label.ssTip,
        handler: 'setSectionWidth',
        params: {
            undo: false,
            needRange: false
        }
    }, {
        icon: 'fa-resize-horizontal',
        title: eConfig.label.resetSectionWidth,
        tip: eConfig.label.sssTip,
        handler: 'resetSectionWidth',
        params: {
            undo: true,
            needRange: false
        }
    }, {
        icon: 'fa-move',
        title: eConfig.label.move,
        tip: eConfig.label.moveTip,
        handler: 'setSectionMove',
        params: {
            undo: false,
            needRange: false
        }
    },
    {
        icon: 'fa-down',
        title: eConfig.label.closedNext,
        tip: eConfig.label.closeNextTip,
        handler: 'closedNext',
        params: {
            undo: false,
            needRange: false
        }
    }, {
        icon: 'fa-doc-text',
        title: eConfig.label.paragraph,
        tip: eConfig.label.paragraphTip,
        handler: 'openParagrapWindow',
        params: {
            undo: false,
            needRange: false
        }
    }]
};
var dropDownControlHtml = "<div style='width:auto;' class='k_edit_tools_tip_wrapper k_edit_tools_drop_wrapper  k_box_shadow'><div class='k_edit_tools_drop_caret'><i class='fa fa-up-dir'></i></div><div class='k_edit_tools_drop_item_wrap'></div></div>";
var defaultOpts = {
    width: 600,
    height: 400,
    undoSize: 20,
    mobile: false,
    onFormCreated: function () { }
};
function getSingleton(className, editor) {
    var key = "_" + className + "_";
    if (window[key]) {
        return window[key];
    }
    var obj = new window[className](editor);
    window[key] = obj;
    return obj;
}